/*
 * @Author: your name
 * @Date: 2021-03-28 09:00:23
 * @LastEditTime: 2021-04-02 13:12:50
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \my-app-koa\app.js
 */
const Koa = require("koa")
const Router = require("koa-router")
const mongoose = require("mongoose")
const bodyParser = require("koa-bodyparser")
const passport = require('koa-passport')


// 实例化 app
const app = new Koa()

// 链接MongoDB
const db = require("./config/keys").mongoURL
mongoose.connect(db,
    { useNewUrlParser: true, useUnifiedTopology: true })
    .then(() => console.log('mongodb in working'))
    .catch(err => console.log(err))

// 配置路由
const router = new Router()
const users = require("./routes/api/users")
const profile = require("./routes/api/profile")
const posts = require("./routes/api/posts")


router.use('/api/users', users)
router.use('/api/profile', profile)
router.use('/api/posts', posts)



router.get('/', async ctx => {
    ctx.body = {
        msg: 'Hello Koa interface'
    }
})


app.use(passport.initialize())
app.use(passport.session())
// 回调到config 文件passport.js
require("./config/passport")(passport)

app.use(bodyParser())
app.use(router.routes()).use(router.allowedMethods());

const port = process.env.PORT || 5000

app.listen(port, () => {
    console.log(`server started on ${port}`)
})